/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.plugin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 注解方式
 * @Intercepts({@Signature(
 *   type= Executor.class,
 *   method = "update",
 *   args = {MappedStatement.class,Object.class})})
 * public class ExamplePlugin implements Interceptor {
 *   private Properties properties = new Properties();
 *   public Object intercept(Invocation invocation) throws Throwable {
 *     // implement pre processing if need
 *     Object returnObject = invocation.proceed();
 *     // implement post processing if need
 *     return returnObject;
 *   }
 *   public void setProperties(Properties properties) {
 *     this.properties = properties;
 *   }
 * }
 * 配置文件方式 mybatis-config.xml
 * <plugins>
 *   <plugin interceptor="org.mybatis.example.ExamplePlugin">
 *     <property name="someProperty" value="100"/>
 *   </plugin>
 * </plugins>
 */
public class InterceptorChain {

  private final List<Interceptor> interceptors = new ArrayList<>();

  public Object pluginAll(Object target) {
    for (Interceptor interceptor : interceptors) {
      target = interceptor.plugin(target);
    }
    return target;
  }

  public void addInterceptor(Interceptor interceptor) {
    interceptors.add(interceptor);
  }

  public List<Interceptor> getInterceptors() {
    return Collections.unmodifiableList(interceptors);
  }

}
